package demo.common.util;

/**
 * 测试多线程时使用
 *
 * @author yexi
 */
public class ThreadUtils {

    public static void execute(Runnable runnable) {
        execute(runnable, 20);
    }

    /**
     * 执行异步任务，且等待所有任务执行完毕
     *
     * @param runnable  异步任务
     * @param threadNum 线程数
     */
    public static void execute(Runnable runnable, int threadNum) {
        Thread[] threads = new Thread[threadNum];
        for (int i = 0; i < threads.length; i++) {
            threads[i] = new Thread(runnable);
        }
        for (Thread t : threads) {
            t.start();
        }
        for (Thread t : threads) {
            try {
                t.join();
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
        }
    }

}
